package com.liser.material.design;

import android.Manifest;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.navigation.NavigationView;
import com.google.gson.Gson;
import com.liser.material.design.framework.helper.SharedPreferencesHelper;
import com.liser.material.design.framework.observer.SimpleObserver;
import com.liser.material.design.framework.ui.BaseActivity;
import com.liser.material.design.net.model.LoginResponse;
import com.liser.material.design.ui.login.LoginActivity;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.liser.material.design.framework.helper.SharedPreferencesHelper.USER_OBJECT;

public class MainActivity extends BaseActivity {

    private static final String TAG = MainActivity.class.getName();

    public static final int REQUEST_CODE = 1225;

    private NavigationView navigationView;

    private MenuItem navLogin;

    private AppBarConfiguration mAppBarConfiguration;

    private MenuItem.OnMenuItemClickListener aboutMenuItemClickListener;

    private MenuItem.OnMenuItemClickListener loginMenuItemClickListener;

    @Override
    protected int onLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onView() {
        // 申请相机权限
        requestSelfPermission(new String[]{Manifest.permission.CAMERA}, (authorize, permissions) -> {
            Log.i(TAG, permissions.toString());
        });

        initToolbar();

        initListener();

        initMemusAndNav();

        initHeaderView();
    }

    private void initToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        /*int screenWidth = QMUIDisplayHelper.getScreenWidth(this);
        mNavButtonView = new AppCompatImageButton(this, null,
                R.attr.toolbarNavigationButtonStyle);
        toolbar.setTitleMarginEnd(screenWidth / 2);
        toolbar.getN*/
        setSupportActionBar(toolbar);
    }

    private void initMemusAndNav() {

        // 浮动按钮
       /* FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(scanClickListener);*/

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_install, R.id.nav_about, R.id.nav_login)
                .setDrawerLayout(drawer)
                .build();

        Menu menu = navigationView.getMenu();
        MenuItem aboutItem = menu.findItem(R.id.nav_about);
        aboutItem.setOnMenuItemClickListener(aboutMenuItemClickListener);

        navLogin = menu.findItem(R.id.nav_login);
        navLogin.setOnMenuItemClickListener(loginMenuItemClickListener);

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    private void initHeaderView() {
        View headerView = navigationView.getHeaderView(0);
        TextView operNameView = headerView.findViewById(R.id.operName);
        TextView branchNameView = headerView.findViewById(R.id.branchName);
        Observable
                .create((ObservableOnSubscribe<String>) emitter -> {
                    String userJson = SharedPreferencesHelper.get(USER_OBJECT, "");
                    emitter.onNext(userJson);
                })
                .map(userJson -> new Gson().fromJson(userJson, LoginResponse.class))
                .compose(bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<LoginResponse>() {
                    @Override
                    public void onSuccess(LoginResponse loginResponse) {
                        operNameView.setText(loginResponse.getOperName());
                        branchNameView.setText(loginResponse.getBranchName());
                        navLogin.setTitle(R.string.action_settings);
                    }
                });

    }

    private void initListener() {
        aboutMenuItemClickListener = menuItem -> {
            Log.i(TAG, "关于点击");
            return true;
        };

        loginMenuItemClickListener = menuItem -> {
            Log.i(TAG, "登录点击");
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            return true;
        };
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        // 顶部右侧菜单栏
//        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        super.setSupportActionBar(toolbar);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp();
    }

    /*@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (MainActivity.REQUEST_CODE == requestCode && Activity.RESULT_OK == resultCode) {
            if (data != null) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                // 跳转到首页
                NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
                navController.navigate(R.id.nav_home, bundle);
            }
        }
    }*/
}
